package org.qydjk.common.util;

import java.security.MessageDigest;

public class MD5Util {
	public static String getMD5Str(String str,String hxKey){
		try {
			MessageDigest md5=MessageDigest.getInstance("md5");
			md5.reset();
			String inputStr=str+hxKey;
			byte[] inputBytes=inputStr.getBytes("utf-8");
			md5.update(inputBytes);
			byte[] result=md5.digest();
			StringBuffer sb=new StringBuffer();
			for (int i = 0; i < result.length; i++) {                
		        if (Integer.toHexString(0xff & result[i]).length() == 1)    
		            sb.append("0").append(Integer.toHexString(0xff & result[i]));    
		        else    
		           sb.append(Integer.toHexString(0xFF & result[i]));    
		    }    
		    return sb.toString();    
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		
	}
	public static String getMD5Str(byte[] input){
		try {
			MessageDigest md5=MessageDigest.getInstance("md5");
			md5.reset();
			md5.update(input);
			byte[] result=md5.digest();
			StringBuffer sb=new StringBuffer();
			for (int i = 0; i < result.length; i++) {                
		        if (Integer.toHexString(0xff & result[i]).length() == 1)    
		            sb.append("0").append(Integer.toHexString(0xff & result[i]));    
		        else    
		           sb.append(Integer.toHexString(0xFF & result[i]));    
		    }    
		    return sb.toString();  
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		
	}
	
	public static String getRandomToken(){
		String uuid = StringUtil.getUUID();
		return getMD5Str(uuid, System.currentTimeMillis()+"");
	}
	public static void main(String[] args) {
		String uuid = StringUtil.getUUID();
		System.out.println(uuid);
		System.out.println(getMD5Str("abc123", uuid));
	}
}
